package com.niit.service.impl;

import com.niit.dao.displayFiles;
import com.niit.pojo.downFile;
import com.niit.service.HadoopService;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.IOException;
import java.net.URI;
import java.util.List;

/**
 * @author 杨铭
 * 2022/2/7,20:45
 */
public class HadoopServiceImpl implements HadoopService {
    Configuration conf = new Configuration();

    @Override
    public void upload(String fileUrl) {
        String resourceUrl = "D:\\Hbase学习\\hadoop-2.7.3";
        String url = "hdfs://192.168.40.133:9000";
        System.setProperty("hadoop.home.dir", resourceUrl);
        try {
            FileSystem fileSystem = FileSystem.get(URI.create(url), conf);
            //本地文件
            Path srcPath = new Path(fileUrl);
            //上传到HDFS
            Path destPath = new Path("/Niit/");
            //使用Hadoop的copyFromLocalFile
            fileSystem.copyFromLocalFile(srcPath, destPath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 为了在下载页面展示出可以下载的内容
     **/
    @Override
    public List<downFile> displayFiles() throws IOException {
        displayFiles displayFiles = new displayFiles();
        List<downFile> downFiles = displayFiles.displayFiles();
        return downFiles;
    }


    public void download(String file_url) throws IOException {
        String resourceUrl = "D:\\Hbase学习\\hadoop-2.7.3";
        String url = "hdfs://192.168.40.133:9000";
        System.setProperty("hadoop.home.dir", resourceUrl);
        FileSystem fileSystem = FileSystem.get(URI.create(url), conf);
        System.out.println(file_url.substring(32));
        fileSystem.copyToLocalFile(false, new Path(file_url.substring(26)), new Path("C:\\Users\\26474\\Desktop"), true);
        // 3 关闭资源
        fileSystem.close();
    }
}
